﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace HuiZe.education.View
{
    /// <summary>
    /// MessageView.xaml 的交互逻辑
    /// </summary>
    public partial class MessageView : UserControl
    {
        public MessageView()
        {
            InitializeComponent();
            Loaded += MessageView_Loaded;
        }
        MessageViewModel messageViewModel { get; set; }
        private void MessageView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            messageViewModel = DataContext as MessageViewModel;
            double height = this.ActualHeight;
            messageViewModel.limit = (int)Math.Floor((height * 0.8 - 50) / 54); //页面显示消息的数量
            messageViewModel.ReqMessage(1);

            PageManages.ItemSourceCount = messageViewModel.pageCount;
            PageManages.UpdataPages();

        }

        private void YCButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MessageModel mag = messageGrid.SelectedItem as MessageModel;
            string showMsg = messageViewModel.GetMessage(mag.id);
            detailTxt.Text = showMsg;
            msgDetail.Visibility = System.Windows.Visibility.Visible;
        }

        private void hideMessageBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            msgDetail.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void PageManages_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            {
                CustomRadioButton button = e.OriginalSource as CustomRadioButton;
                string index = button.Content.ToString();
                if (index == ">")
                {
                    if (PageManages.CurrentIndex + 1 <= PageManages.ItemSourceCount)
                        //index = pageManage.Content.ToString();
                        index = PageManages.CurrentIndex < PageManages.ItemSourceCount ? (PageManages.CurrentIndex + 1).ToString() : PageManages.CurrentIndex.ToString();
                    else
                        index = PageManages.ItemSourceCount.ToString();
                }
                if (index == "<")
                {
                    if (PageManages.CurrentIndex - 1 >= 1)

                        index = PageManages.CurrentIndex > 1 ? (PageManages.CurrentIndex - 1).ToString() : "1";
                    else
                        index = "1";
                }
                if (index == "首页")
                {
                    index = "1";
                }
                if (index == "尾页")
                {
                    index = PageManages.ItemSourceCount.ToString();
                }


                messageViewModel.ReqMessage(Convert.ToInt32(index));
                PageManages.CurrentIndex = Convert.ToInt32(index);
                PageManages.ItemSourceCount = messageViewModel.pageCount;
                PageManages.UpdataPages();

                //foreach (var control in PageManages.uniformGrid.Children)
                //{
                //    var child = control as CustomRadioButton;

                //    if (child.Content.ToString() == index.ToString())
                //    {
                //        child.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55E4C2"));
                //        child.SelectedBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55E4C2"));
                //        child.IsChecked = true;
                //    }
                //}




            }
        }
    }
}
